package com.social.service.impl;

import cn.hutool.core.date.LocalDateTimeUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.social.dao.NoticeDao;
import com.social.pojo.Notice;
import com.social.pojo.NoticeDetail;
import com.social.pojo.Result;
import com.social.service.NoticeDetailService;
import com.social.service.NoticeService;
import com.social.utils.CacheClient;
import com.social.utils.RedisIdWorker;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

import java.time.LocalDateTime;
import java.util.List;
import java.util.concurrent.TimeUnit;

import static com.social.constant.MessageConstant.OPERATE_DATA_FAIL;
import static com.social.constant.MessageConstant.OPERATE_DATA_SUCCESS;
import static com.social.constant.RedisConstants.*;

/**
 * @className: NoticeServiceImpl
 * @description: TODO 类描述
 * @author: XiaoXu
 * @date: 2023/01/09 19:01
 **/
@Service
public class NoticeServiceImpl extends ServiceImpl<NoticeDao, Notice> implements NoticeService {

    @Resource
    private NoticeDetailService noticeDetailService;

    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @Resource
    private NoticeDao noticeDao;
    @Resource
    private CacheClient cacheClient;

    @Resource
    private RedisIdWorker redisIdWorker;
    @Override
    @Transactional
    public Result modify(Notice notice) {
        NoticeDetail detail = new NoticeDetail();
        detail.setId(notice.getContent_id());
        detail.setContent(notice.getContent());
        noticeDetailService.updateById(detail);
        updateById(notice);
        return Result.ok(OPERATE_DATA_SUCCESS);
    }

    @Override
    @Transactional
    public Result insert(Notice notice) {
        NoticeDetail detail = new NoticeDetail();
        detail.setContent(notice.getContent());
        long id = redisIdWorker.nexId(NOTICE_ID_PREFIX);
        detail.setId(id);
        boolean save = noticeDetailService.save(detail);
        notice.setPublic_time(LocalDateTimeUtil.now());
        notice.setContent_id(id);
        save(notice);
        return Result.ok(OPERATE_DATA_SUCCESS);
    }

    @Override
    public Result getNoticeById(Long id) {
        Notice notice = cacheClient.queryWithPassThrough(NOTICE_ID_KEY, id, Notice.class, idx -> getById(id), NOTICE_ID_TTL, TimeUnit.MINUTES);
        if (notice == null){
            return Result.fail(OPERATE_DATA_FAIL,"公共已删除");
        }
        NoticeDetail detail = noticeDetailService.getById(notice.getContent_id());
        notice.setContent(detail.getContent());
        return Result.ok(OPERATE_DATA_SUCCESS,notice);
    }

    @Override
    public Result getNoticeList(Long size, Long index) {
        QueryWrapper<Notice> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("public_time");
        Page<Notice> page = noticeDao.selectPage(new Page<>(index, size), wrapper);
        List<Notice> records = page.getRecords();
        if (records == null){
            return Result.fail(OPERATE_DATA_SUCCESS,"查询失败");
        }
        return Result.ok(OPERATE_DATA_SUCCESS,records);
    }
}
